package com.ruicar.afs.cloud.basic.proceeds.pay.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.proceeds.pay.condition.AutoPayConfigCondition;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicAutoPaymentConfig;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicAutoPaymentConfigService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.YesOrNoEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-07-27 18:45
 */
@Slf4j
@RestController
@Api("自动付款配置管理")
@RequestMapping("/autoPay")
@AllArgsConstructor
public class AutoPayManagementController {

    private final BasicAutoPaymentConfigService basicAutoPaymentConfigService;



    @PostMapping(value = "/saveAutoPayInfo")
    @ApiOperation(value = "保存自动付款配置信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<BasicAutoPaymentConfig> deleteDebitBatch(@ModelAttribute AutoPayConfigCondition autoPaymentConfig) {

        /**  开始时间结束时间不跨天 by ZC.GUO  **/
        if (autoPaymentConfig.getWorkStartDate()!= null && autoPaymentConfig.getWorkEndDate() != null){
            if (!DateUtil.isSameDay(autoPaymentConfig.getWorkStartDate(),autoPaymentConfig.getWorkEndDate())){
                return IResponse.fail("工作起止时间不是同一天，保存失败");
            }
        }

        /**  信息提取 by ZC.GUO  **/
        BasicAutoPaymentConfig basicAutoPaymentConfig = new BasicAutoPaymentConfig();
        basicAutoPaymentConfig.setWorkStartDate(autoPaymentConfig.getWorkStartDate());
        basicAutoPaymentConfig.setWorkEndDate(autoPaymentConfig.getWorkEndDate());
        basicAutoPaymentConfig.setInterBankTransferBank(autoPaymentConfig.getInterBankTransferBank());
        basicAutoPaymentConfig.setBatchMaxNum(autoPaymentConfig.getBatchMaxNum());
        basicAutoPaymentConfig.setNoAutoBank(autoPaymentConfig.getNoAutoBank());
        basicAutoPaymentConfig.setWorkStartTime(autoPaymentConfig.getWorkStartTime());
        basicAutoPaymentConfig.setWorkEndTime(autoPaymentConfig.getWorkEndTime());
        basicAutoPaymentConfig.setEnableAutoPay(autoPaymentConfig.getEnableAuto()? YesOrNoEnum.yes:YesOrNoEnum.no);

        /**  如果已有信息，则删除 by ZC.GUO  **/
        if (EmptyUtils.isNotEmpty(autoPaymentConfig.getId())){
            basicAutoPaymentConfigService.removeById(autoPaymentConfig.getId());
        }
        /**  保存最新配置 by ZC.GUO  **/
        basicAutoPaymentConfigService.save(basicAutoPaymentConfig);

        /**  最新配置信息反显 by ZC.GUO  **/
        if (EmptyUtils.isNotEmpty(basicAutoPaymentConfig.getNoAutoBank())){
            basicAutoPaymentConfig.setBankList(new ArrayList<>(Arrays.asList(basicAutoPaymentConfig.getNoAutoBank().split(","))));
        }
        basicAutoPaymentConfig.setEnableAuto(YesOrNoEnum.yes.equals(basicAutoPaymentConfig.getEnableAutoPay()));
        return IResponse.success(basicAutoPaymentConfig);
    }

    @PostMapping(value = "/queryAutoPayInfo")
    @ApiOperation(value = "查询自动付款配置信息")
    public IResponse<BasicAutoPaymentConfig> queryAutoPayInfo(@RequestParam("id") String id) {

        /**  信息提取 by ZC.GUO  **/
        BasicAutoPaymentConfig basicAutoPaymentConfig = basicAutoPaymentConfigService.getOne(Wrappers.<BasicAutoPaymentConfig>query().lambda());
        if(EmptyUtils.isEmpty(basicAutoPaymentConfig)){
            basicAutoPaymentConfig = new BasicAutoPaymentConfig();
        }else {
            if (EmptyUtils.isNotEmpty(basicAutoPaymentConfig.getNoAutoBank())){
                basicAutoPaymentConfig.setBankList(new ArrayList<>(Arrays.asList(basicAutoPaymentConfig.getNoAutoBank().split(","))));
            }
            basicAutoPaymentConfig.setEnableAuto(YesOrNoEnum.yes.equals(basicAutoPaymentConfig.getEnableAutoPay()));
        }
        return IResponse.success(basicAutoPaymentConfig);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
